/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.action.query;

import com.inspur.edp.bff.api.manager.context.DataMapperContext;
import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.bff.spi.AbstractFSAction;
import com.inspur.edp.cef.entity.dependenceTemp.Pagination;
import com.inspur.edp.cef.entity.entity.IEntityData;

import java.util.ArrayList;
import java.util.HashMap;

public class QoQueryAction extends AbstractFSAction<ArrayList<IEntityData>> {
    private IEntityData param;
    private Pagination pageInfo;
    private String extendCond;

    public QoQueryAction(IEntityData param, Pagination pageInfo, String extendCond) {
        this.param = param;
        this.pageInfo = pageInfo;
        this.extendCond = extendCond;
    }

    @Override
    protected void execute() {
        if (getContext().getAssemblerManager() == null) {
            doDefaultQuery();
            return;
        }

        QueryContext queryContext = new QueryContext();
        queryContext.setPageInfo(pageInfo);
        queryContext.setParam(param);
        queryContext.setExtendCond(extendCond);

        //① BeforeQuery
        getContext().getAssemblerManager().getQueryAssembler().doBeforeQuery(getContext(), queryContext);

        //② DoQuery
        HashMap<String, ArrayList<IEntityData>> queryResult = getContext().getAssemblerManager().
                getQueryAssembler().doQuery(getContext(), queryContext);
        //③ DataMapping
        DataMapperContext mapperContext = new DataMapperContext();
        mapperContext.setSourceData(queryResult);

        ArrayList<IEntityData> vmDatas = getContext().getAssemblerManager().
                getDataMapperAssembler().doDataMapping2Vo(getContext(), mapperContext);
        queryContext.setQueryResult(vmDatas);
        //④ AfterQuery
        getContext().getAssemblerManager().getQueryAssembler().doAfterQuery(getContext(), queryContext);
        setResult(vmDatas);
    }

    private void doDefaultQuery() {
    }
}
